<template>
  <div>
    <el-form :inline="true" :model="queryParams" style="margin-top: 20px; height: 33px">
      <el-form-item label="卖方公司名称：" style="margin-left: 30px; font-weight: bold">
        <el-input v-model="queryParams.sellerName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="买方公司名称：" style="margin-left: 20px; font-weight: bold">
        <el-input v-model="queryParams.buyerName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit1()">查询</el-button>
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button type="success" @click="onSubmit2()">导出所有记录</el-button>
      </el-form-item>
    </el-form>
    <el-main>
      <el-scrollbar>
        <el-table :data="tableData" stripe style="width: 100%" max-height="300">
          <el-table-column fixed type="index" label="序号" width="70" align="center" />
          <el-table-column prop="sellerName" label="卖方公司名称" width="200" align="center" />
          <el-table-column prop="buyerName" label="买方公司名称" width="200" align="center" />
          <el-table-column prop="tradeTime" label="交易日期" width="180" align="center">
            <template #default="scope">
              <div style="display: flex; align-items: center">
                <span style="margin-left: 10px">{{
                  moment(scope.row.tradeTime).format('YYYY-MM-DD  h:mm')
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="item" label="交易商品" width="180" align="center" />
          <el-table-column prop="amount" label="交易额度" align="center" />
        </el-table>
      </el-scrollbar>
    </el-main>
  </div>
  <div>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-count="totalPage"
      @change="doPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { CompanyUserControllerService, TradeControllerService } from '../../../../generated'
import moment from 'moment'

const totalPage = ref(10)

interface Data {
  sellerName: ''
  buyerName: ''
  tradeTime: ''
  item: '碳排放额度'
  amount: ''
}

const tableData = ref<Data>()

// 查询参数
const queryParams = ref({
  current: 0,
  pageSize: 10,
  sellerName: '',
  buyerName: ''
})
const loadData = async () => {
  const res = await TradeControllerService.listTradeVoByPageUsingPost(queryParams.value)
  // console.log(res.data.records)
  tableData.value = res.data.records
  for (let i in res.data.records) {
    const res2 = await CompanyUserControllerService.getByUserIdUsingPost(
      res.data.records[i].sellerId
    )
    const res3 = await CompanyUserControllerService.getByUserIdUsingPost(
      res.data.records[i].buyerId
    )
    // console.log(res2.data)
    tableData.value[i].sellerName = res2.data.companyName
    tableData.value[i].buyerName = res2.data.companyName
    tableData.value[i].item = '碳排放额度'
  }

  totalPage.value = Math.floor(
    (Number(queryParams.value.pageSize - 1) + Number(res.data.total)) /
      Number(queryParams.value.pageSize)
  )
}

onMounted(() => {
  loadData()
})
/**
 * 分页请求
 * @param page
 */
const doPageChange = (page: any) => {
  queryParams.value = page
}

/**
 * 查询
 */
function onSubmit1() {
  console.log(queryParams.value)
}

function onSubmit2() {}
</script>

<style scoped>
.pagination-style {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
